
#include "max31865.h"


DigitalOut max31865_cs(PD_2);
DigitalOut max31865_sclk(PC_11);
DigitalOut max31865_sdi(PC_10);
DigitalIn max31865_sdo(PC_12);

DigitalOut max31865_cs2(PA_13);
DigitalOut max31865_cs3(PA_14);

unsigned char sdi_buf;

void test(unsigned int *cs,unsigned int *sclk,unsigned int *sdi)
{
    *cs=1;   
    *sclk=1;
    *sdi=1;
  
    *cs=0;   
    *sclk=0;
    *sdi=0;

}

//void	test1(void)
//{
//	test(&max31865_cs2,&max31865_sclk2,&max31865_sdi2);

//}

void max31865_init(void)
{
    max31865_cs=1;
    max31865_sclk=0;
    max31865_sdi=1;
  //  max31865_write_data(0x80,0x84);//config Fault detection with automatic delay    
}

void max31865_write_type(unsigned char data)
{
    unsigned int i;
    
    for(i=0;i<8;i++)
    { 
        max31865_sclk=0;

        max31865_sdi=((data&(0x80>>i))?1:0);
                sdi_buf<<=1;
                sdi_buf|=max31865_sdi;
        max31865_sclk=1;
    }     
            max31865_sclk=0;
}
unsigned char max31865_read_data(unsigned char chx, unsigned char add)
{
    unsigned char i,out_data;
		switch(chx)
		{
			case 1:max31865_cs=1;break;
			case 2:max31865_cs2=1;break;
			case 3:max31865_cs3=1;break;
		}
    max31865_sclk=0;
		switch(chx)
		{
			case 1:max31865_cs=0;break;
			case 2:max31865_cs2=0;break;
			case 3:max31865_cs3=0;break;
		}
    max31865_write_type(add);
    out_data=0;
    for(i=0;i<8;i++)
    {
        max31865_sclk=0;
        max31865_sclk=1;
        out_data<<=1;
        out_data|=max31865_sdo;
            
    }       
		switch(chx)
		{
			case 1:max31865_cs=1;break;
			case 2:max31865_cs2=1;break;
			case 3:max31865_cs3=1;break;
		}

    return out_data;
}

void max31865_write_data(unsigned char chx,unsigned char add,unsigned char data)
{
		switch(chx)
		{
			case 1:max31865_cs=1;break;
			case 2:max31865_cs2=1;break;
			case 3:max31865_cs3=1;break;
		}
    max31865_sclk=0;
		switch(chx)
		{
			case 1:max31865_cs=0;break;
			case 2:max31865_cs2=0;break;
			case 3:max31865_cs3=0;break;
		}
    max31865_write_type(add);
    max31865_write_type(data);
		switch(chx)
		{
			case 1:max31865_cs=1;break;
			case 2:max31865_cs2=1;break;
			case 3:max31865_cs3=1;break;
		}

}